{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1f42b856-6cd2-4cdf-a1ba-e609123c0e0b",
   "metadata": {},
   "source": [
    "# Lesson 1 - Your first generations with Amazon Bedrock"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad93ca27-aeef-47fe-8b91-dcbc82ed5824",
   "metadata": {},
   "source": [
    "Welcome to Lesson 1. \n",
    "\n",
    "You'll start with using Amazon Bedrock to prompt a model and customize how it generates its response.\n",
    "\n",
    "**Note:** To access the `requirements.txt` file, go to `File` and click on `Open`. Here, you will also find all helpers functions and datasets used in each lesson.\n",
    " \n",
    "I hope you enjoy this course!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7da4cea-119c-4c59-971e-852713490dc4",
   "metadata": {},
   "source": [
    "### Import all needed packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c74fc7b",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "import boto3\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64361ba9-7b91-421e-a26c-d23f50f4c6dd",
   "metadata": {},
   "source": [
    "### Setup the Bedrock runtime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "887b8bc3",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "bedrock_runtime = boto3.client('bedrock-runtime', region_name='us-west-2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eaa97fc",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "prompt = \"Write a one sentence summary of Las Vegas.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab2f617d",
   "metadata": {
    "height": 182
   },
   "outputs": [],
   "source": [
    "kwargs = {\n",
    "    \"modelId\": \"amazon.titan-text-lite-v1\",\n",
    "    \"contentType\": \"application/json\",\n",
    "    \"accept\": \"*/*\",\n",
    "    \"body\": json.dumps(\n",
    "        {\n",
    "            \"inputText\": prompt\n",
    "        }\n",
    "    )\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "595f287f",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "response = bedrock_runtime.invoke_model(**kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42e42b9b",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b54099e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "response_body = json.loads(response.get('body').read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ab4dfd3",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(json.dumps(response_body, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87b190ba",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(response_body['results'][0]['outputText'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "417a6b99-4183-43fe-8eca-f6ad4be714de",
   "metadata": {},
   "source": [
    "### Generation Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09de166f",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "prompt = \"Write a summary of Las Vegas.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fffec72c",
   "metadata": {
    "height": 267
   },
   "outputs": [],
   "source": [
    "kwargs = {\n",
    "    \"modelId\": \"amazon.titan-text-express-v1\",\n",
    "    \"contentType\": \"application/json\",\n",
    "    \"accept\": \"*/*\",\n",
    "    \"body\" : json.dumps(\n",
    "        {\n",
    "            \"inputText\": prompt,\n",
    "            \"textGenerationConfig\": {\n",
    "                \"maxTokenCount\": 100,\n",
    "                \"temperature\": 0.7,\n",
    "                \"topP\": 0.9\n",
    "            }\n",
    "        }\n",
    "    )\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f0a1194",
   "metadata": {
    "height": 97
   },
   "outputs": [],
   "source": [
    "response = bedrock_runtime.invoke_model(**kwargs)\n",
    "response_body = json.loads(response.get('body').read())\n",
    "\n",
    "generation = response_body['results'][0]['outputText']\n",
    "print(generation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01cfb22f",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(json.dumps(response_body, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b6e20f2-6f5d-4f25-9eca-15ebd98c1bc6",
   "metadata": {
    "height": 267
   },
   "outputs": [],
   "source": [
    "kwargs = {\n",
    "    \"modelId\": \"amazon.titan-text-express-v1\",\n",
    "    \"contentType\": \"application/json\",\n",
    "    \"accept\": \"*/*\",\n",
    "    \"body\" : json.dumps(\n",
    "        {\n",
    "            \"inputText\": prompt,\n",
    "            \"textGenerationConfig\": {\n",
    "                \"maxTokenCount\": 500,\n",
    "                \"temperature\": 0.7,\n",
    "                \"topP\": 0.9\n",
    "            }\n",
    "        }\n",
    "    )\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20399cd0-99c5-4458-9cb0-d511a3b03741",
   "metadata": {
    "height": 97
   },
   "outputs": [],
   "source": [
    "response = bedrock_runtime.invoke_model(**kwargs)\n",
    "response_body = json.loads(response.get('body').read())\n",
    "\n",
    "generation = response_body['results'][0]['outputText']\n",
    "print(generation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "547909cd-29f3-4b4b-976c-aa933e2964b6",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(json.dumps(response_body, indent=4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93efa22f",
   "metadata": {},
   "source": [
    "### Working with other type of data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f582bfd4",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "from IPython.display import Audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ed7cda6",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "audio = Audio(filename=\"dialog.mp3\")\n",
    "display(audio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f494d3e",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "with open('transcript.txt', \"r\") as file:\n",
    "    dialogue_text = file.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99500185",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(dialogue_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ad6bd54",
   "metadata": {
    "height": 165
   },
   "outputs": [],
   "source": [
    "prompt = f\"\"\"The text between the <transcript> XML tags is a transcript of a conversation. \n",
    "Write a short summary of the conversation.\n",
    "\n",
    "<transcript>\n",
    "{dialogue_text}\n",
    "</transcript>\n",
    "\n",
    "Here is a summary of the conversation in the transcript:\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a4e8336",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26853371",
   "metadata": {
    "height": 267
   },
   "outputs": [],
   "source": [
    "kwargs = {\n",
    "    \"modelId\": \"amazon.titan-text-express-v1\",\n",
    "    \"contentType\": \"application/json\",\n",
    "    \"accept\": \"*/*\",\n",
    "    \"body\": json.dumps(\n",
    "        {\n",
    "            \"inputText\": prompt,\n",
    "            \"textGenerationConfig\": {\n",
    "                \"maxTokenCount\": 512,\n",
    "                \"temperature\": 0,\n",
    "                \"topP\": 0.9\n",
    "            }\n",
    "        }\n",
    "    )\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cb963a0",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "response = bedrock_runtime.invoke_model(**kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71429d89",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "response_body = json.loads(response.get('body').read())\n",
    "generation = response_body['results'][0]['outputText']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e6db611",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(generation)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
